package com.djp.smartapp.dialog;

import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.djp.smartapp.MainActivity;
import com.djp.smartapp.bean.NoDataResponse;
import com.djp.smartapp.bean.UserLineResponse;
import com.djp.smartapp.util.Line;
import com.djp.smartapp.util.LineSpUtil;
import com.lenovo.manufacture.R;

public class CreateLineDialog extends MyDialog {
    private static final String TAG = "CreateLineDialog";
    private EditText etLinePosition;
    private EditText etLineType;
    private int linePosition;
    private int lineType;

    protected CreateLineDialog(MainActivity context) {
        super(context);
    }

    @Override
    protected int setAlertLayout() {
        return R.layout.dialog_create_line;
    }

    @Override
    protected int setCancelButton() {
        return R.id.dialog_btn_create_line_cancel;
    }

    @Override
    protected int setFinishButton() {
        return R.id.dialog_btn_create_line;
    }

    @Override
    protected void setFinishButtonOnClick() {
        if (submit()) {
            sendReq();
        }
    }

    @Override
    protected void initView(View view) {
        etLinePosition = view.findViewById(R.id.dialog_et_line_position);
        etLineType = view.findViewById(R.id.dialog_et_line_type);
    }

    private void sendReq() {
        new Thread() {
            @Override
            public void run() {
                //先判断是否已经创建了一条生产线
                if (!Line.lineIsReady(context)) {
                    String reqBody = "lineId=" + lineType + "&" + "pos=" + linePosition;
                    //Log.d(TAG, "run: "+reqBody);
                    String response = okHttpUtil.getPostRes("index/createStudentLine", reqBody);
                    NoDataResponse noDataResponse = gson.fromJson(response, NoDataResponse.class);
                    if (noDataResponse.getStatus().equals("200")) {

                        //发送请求按位置查询创建的生产线是哪一条
                        String searchUserLine = okHttpUtil.getPostRes("UserProductionLine/search", "position=" + linePosition);
                        UserLineResponse userLineResponse = gson.fromJson(searchUserLine, UserLineResponse.class);

                        if (userLineResponse.getStatus().equals("200")) {
                            //查询成功后将创建的生产线的信息存储到SharedPreferences中
                            LineSpUtil.setLine(
                                    userLineResponse.getData().get(0).getId(),
                                    userLineResponse.getData().get(0).getProductionLineId(),
                                    context
                            );
                            context.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    toast("创建生产线成功");
                                    alertDialog.dismiss();
                                }
                            });
                        }
                    }
                } else {
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            alertDialog.dismiss();
                            Log.d(TAG, "setFinishButtonOnClick: 已经有一条生产线了，请直接操作");
                            toast("已经有一条生产线了，请直接操作");
                        }
                    });
                }
            }
        }.start();
    }

    private boolean submit() {
        String mLineType = etLineType.getText().toString();
        String mLinePosition = etLinePosition.getText().toString();
        if (!mLineType.equals("") && !mLinePosition.equals("")) {
            if (Integer.parseInt(mLinePosition) > 3 && Integer.parseInt(mLinePosition) < 0) {
                toast("生产线位置范围错误");
                return false;
            } else if (Integer.parseInt(mLineType) > 3 && Integer.parseInt(mLineType) < 1) {
                toast("生产线类型范围错误");
                return false;
            } else {
                linePosition = Integer.parseInt(mLinePosition);
                lineType = Integer.parseInt(mLineType);
                return true;
            }
        } else {
            toast("任意项都不能为空");
            return false;
        }
    }
}
